﻿using TestBat.IModels.Domain;
using Iesi.Collections.Generic;
using System;

namespace TestBat.Models.Domain
{
    public class MultipleResponseSolution : ChoiceSolution, IMultipleResponseSolution
    {
        public MultipleResponseSolution()
        {
            Picks = new HashedSet<ITestChoice>();
        }

        public override int? GetPercentage()
        {
            int numChoices = (Task as ITestMultipleResponseTask).Choices.Count;
            if (numChoices == 0)
                return 0;

            MultipleResponseTaskGrading grading = (Task as ITestMultipleResponseTask).Grading;
            switch (grading)
            {
                case MultipleResponseTaskGrading.All:
                {
                    foreach (var choice in (Task as ITestMultipleResponseTask).Choices)
                        if ((choice.Correct && !Picks.Contains(choice)) || (!choice.Correct && Picks.Contains(choice)))
                            return 0;
                    return 100;
                }
                case MultipleResponseTaskGrading.Ratio:
                {
                    int correctChoices = 0;
                    foreach (var choice in (Task as ITestMultipleResponseTask).Choices)
                        if ((choice.Correct && Picks.Contains(choice)) || (!choice.Correct && !Picks.Contains(choice)))
                            correctChoices++;

                    return (int)Math.Floor(((float)correctChoices) / (float)numChoices * 100.0f);
                }
                case MultipleResponseTaskGrading.PlusMinus:
                {
                    int correctChoices = 0;
                    foreach (var choice in (Task as ITestMultipleResponseTask).Choices)
                        if ((choice.Correct && Picks.Contains(choice)) || (!choice.Correct && !Picks.Contains(choice)))
                            correctChoices++;

                    int incorrectChoices = 0;
                    foreach (var choice in (Task as ITestMultipleResponseTask).Choices)
                        if ((!choice.Correct && Picks.Contains(choice)) || (choice.Correct && !Picks.Contains(choice)))
                            incorrectChoices++;

                    int choices = Math.Max(0, (correctChoices - incorrectChoices));

                    return (int)Math.Floor(((float)choices) / (float)numChoices * 100.0f);
                }
                default:
                throw new NotImplementedException();
            }
        }

        public override bool IsEmtpy
        {
            get
            {
                return Picks.Count == 0;
            }
        }

        public virtual ISet<ITestChoice> Picks
        {
            get;
            private set;
        }
    }
}
